package de.java.shortys.myhome.api.view;

import static de.java.shortys.myhome.viewconfigurator.configuration.ViewConfigurationConstants.SWINGMODULEVIEW_BACKGROUND;

import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;

import de.java.shortys.myhome.viewconfigurator.controller.ViewConfigurationManager;

public class SwingModuleViewTree extends JTree {

	private DefaultTreeModel model;
	private DefaultMutableTreeNode root;

	public DefaultMutableTreeNode getRoot() {
		return root;
	}

	public SwingModuleViewTree() {
		initialize();

	}

	public void add(DefaultMutableTreeNode node) {
		root.add(node);
		reload(root);
	}

	public void reload() {
		model.reload();
	}

	public void reload(TreeNode node) {
		model.reload(node);
	}

	private void initialize() {
		setRowHeight(25);
		setCellRenderer(new SwingModuleViewTreeCellRenderer());
		setUIManagerProperties();
		setOpaque(false);
		setUI(new SwingModuleViewTreeUI());
		setModel();
		setToggleClickCount(1);
		setRootVisible(false);
	}

	private void setRoot() {
		root = new DefaultMutableTreeNode("Root");
	}

	private void setModel() {
		setRoot();
		model = new DefaultTreeModel(root);
		super.setModel(model);
	}

	private void setUIManagerProperties() {
		UIManager.put("ScrollBar.background", ViewConfigurationManager.getInstance().getColor(
				SWINGMODULEVIEW_BACKGROUND));
		UIManager.put("ScrollBar.darkShadow", ViewConfigurationManager.getInstance().getColor(
				SWINGMODULEVIEW_BACKGROUND));
		UIManager.put("ScrollBar.highlight", ViewConfigurationManager.getInstance()
				.getColor(SWINGMODULEVIEW_BACKGROUND));
		UIManager.put("ScrollBar.shadow", ViewConfigurationManager.getInstance().getColor(SWINGMODULEVIEW_BACKGROUND));
		UIManager.put("ScrollBar.thumbShadow", ViewConfigurationManager.getInstance().getColor(
				SWINGMODULEVIEW_BACKGROUND));
		UIManager.put("ScrollBar.foreground", ViewConfigurationManager.getInstance().getColor(
				SWINGMODULEVIEW_BACKGROUND));
		UIManager.put("ScrollBar.thumb", ViewConfigurationManager.getInstance().getColor(SWINGMODULEVIEW_BACKGROUND));
		UIManager.put("ScrollBar.thumbDarkShadow", ViewConfigurationManager.getInstance().getColor(
				SWINGMODULEVIEW_BACKGROUND));
		UIManager.put("ScrollBar.thumbHighlight", ViewConfigurationManager.getInstance().getColor(
				SWINGMODULEVIEW_BACKGROUND));
		UIManager.put("ScrollBar.track", ViewConfigurationManager.getInstance().getColor(SWINGMODULEVIEW_BACKGROUND));
		UIManager.put("ScrollBar.trackHighlight", ViewConfigurationManager.getInstance().getColor(
				SWINGMODULEVIEW_BACKGROUND));
		UIManager.put("ScrollBar.background", ViewConfigurationManager.getInstance().getColor(
				SWINGMODULEVIEW_BACKGROUND));
		UIManager.put("control", ViewConfigurationManager.getInstance().getColor(SWINGMODULEVIEW_BACKGROUND));
	}
}
